package cn.iocoder.yudao.module.alipay.service.infothirdaliapp;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.alipay.controller.admin.infothirdaliapp.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.infothirdaliapp.InfoThirdAliAppDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.alipay.convert.infothirdaliapp.InfoThirdAliAppConvert;
import cn.iocoder.yudao.module.alipay.dal.mysql.infothirdaliapp.InfoThirdAliAppMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.alipay.enums.ErrorCodeConstants.*;

/**
 * 第三方应用信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class InfoThirdAliAppServiceImpl implements InfoThirdAliAppService {

    @Resource
    private InfoThirdAliAppMapper infoThirdAliAppMapper;

    @Override
    public Long createInfoThirdAliApp(InfoThirdAliAppCreateReqVO createReqVO) {
        // 插入
        InfoThirdAliAppDO infoThirdAliApp = InfoThirdAliAppConvert.INSTANCE.convert(createReqVO);
        infoThirdAliAppMapper.insert(infoThirdAliApp);
        // 返回
        return infoThirdAliApp.getId();
    }

    @Override
    public void updateInfoThirdAliApp(InfoThirdAliAppUpdateReqVO updateReqVO) {
        // 校验存在
        validateInfoThirdAliAppExists(updateReqVO.getId());
        // 更新
        InfoThirdAliAppDO updateObj = InfoThirdAliAppConvert.INSTANCE.convert(updateReqVO);
        infoThirdAliAppMapper.updateById(updateObj);
    }

    @Override
    public void deleteInfoThirdAliApp(Long id) {
        // 校验存在
        validateInfoThirdAliAppExistsByAppId(id);
        // 删除
        infoThirdAliAppMapper.deleteById(id);
    }

    private void validateInfoThirdAliAppExistsByAppId(Long appId) {
        if (infoThirdAliAppMapper.selectByAppId(appId) == null) {
            throw exception(INFO_THIRD_ALI_APP_NOT_EXISTS);
        }
    }

    private void validateInfoThirdAliAppExists(Long id) {
        if (infoThirdAliAppMapper.selectById(id) == null) {
            throw exception(INFO_THIRD_ALI_APP_NOT_EXISTS);
        }
    }

    @Override
    public InfoThirdAliAppDO getInfoThirdAliApp(Long id) {
        return infoThirdAliAppMapper.selectById(id);
    }

    @Override
    public List<InfoThirdAliAppDO> getInfoThirdAliAppList(Collection<Long> ids) {
        return infoThirdAliAppMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InfoThirdAliAppDO> getInfoThirdAliAppPage(InfoThirdAliAppPageReqVO pageReqVO) {
        return infoThirdAliAppMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InfoThirdAliAppDO> getInfoThirdAliAppList(InfoThirdAliAppExportReqVO exportReqVO) {
        return infoThirdAliAppMapper.selectList(exportReqVO);
    }

}
